﻿/*------------------------------------------------------------------------

C3DMEM.HPP v1.0.1

Copyright 楊志賢 CxxlMan, 2012 - 2013
All Rights Reserved

------------------------------------------------------------------------*/

#if !defined(__C3DMEM_HPP_Cxxl3D)
#define __C3DMEM_HPP_Cxxl3D


#include <stdlib.h>
#include <SYSSELEC.HPP>


namespace Cxxl3D
{

// 配置指定大小的記憶體 並指定對齊位址的間格大小
inline void * cxxlFASTCALL AllocateAlignedMemory(size_t size, size_t Alignedsize = 16)
{
  if( size==0 ) return NULL;

  unsigned char *pblock = (unsigned char *)malloc(size + Alignedsize);

 	if ( pblock == NULL) return NULL;

  unsigned int * aligned = (unsigned int *) ((int)(pblock + Alignedsize + 4) & ~(Alignedsize-1));
  aligned[-1] = (unsigned int) pblock;

  return aligned;
}

// 釋放由 AllocateAlignedMemory 配置的記憶體
inline void cxxlFASTCALL ReleaseAlignedMemory(void *memory)
{
  if( memory==NULL ) return;

	unsigned int *pMemory = (unsigned int *)memory;
	free( (void *)pMemory[-1] );
}

}   /* namespace Cxxl3D */
#endif
